package com.societyGames.interceptExercises
{
import flash.utils.Dictionary;

public class FunctionCallExpression implements INumberExpression
{
  private static var _functionNameToValue:Dictionary = new Dictionary(); //name:String, value:Number

  private var _functionName:String;

  public function FunctionCallExpression(xml:XML)
  {
    if (!"value" in xml)
    {
      throw new ArgumentError("Xml missing required attribute \"value\".");
    }
    this._functionName = xml.@value;
  }

  public function toString():String
  {
    if (this._functionName != null)
    {
      return _functionName + "()";
    }
    return "";
  }

  //INumberExpression-->
  public function evaluate():Number
  {
    if (!_functionNameToValue.hasOwnProperty(_functionName))
    {
      throw new Error("FunctionCallExpression invoked unknown function: " + _functionName);
    }
    return _functionNameToValue[_functionName];
  }

  //<--INumberExpression

  public static function setFunctionReturnValue(functionName:String, returnValue:Number):void
  {
    _functionNameToValue[functionName] = returnValue;
  }

  public static function getFunctionReturnValue(functionName:String):Number
  {
    if (!_functionNameToValue.hasOwnProperty(functionName))
    {
      throw new Error("FunctionCallExpression invoked unknown function: " + functionName);
    }
    return _functionNameToValue[functionName];
  }
}
}